let id = 0;
// 发布订阅
class Dep{

  constructor(){
    this.id = id++;
    this.subs = [];
  }

  addSub(watcher){
    // 订阅
    this.subs.push(watcher);
  }

  notify(){
    this.subs.forEach(watcher => watcher.update());
  }

  depend(){
    if (Dep.target) {
      //Dep.target是一个渲染watcher 
      // 让watcher有 dep, dep上有watcher
      Dep.target.addDep(this);
    }
  }
}
// 来收集依赖， 收集的是watcher

let stack = [];
export function pushTarget(watcher){
  Dep.target = watcher;
  stack.push(watcher);
}

export function popTarget(){
  stack.pop();
  Dep.target = stack[stack.length - 1];
}

export default Dep;